package 笔试.迅雷;

import java.util.HashMap;
import java.util.Scanner;

public class 最大优雅度 {
    public static int max = 0;
    public static int n = 0;
    public static int k = 0;
    public static int[][] items;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        k = in.nextInt();
        items = new int[n][2];
        for (int i = 0; i < n; i++) {
            items[i][0] = in.nextInt();
            items[i][1] = in.nextInt();
        }
        int sum = 0;
        int dept = 0;
        int index = 0;
        HashMap<Integer, Integer> cur = new HashMap<>();
        solve(cur, index, sum, dept);
        System.out.println(max);
    }

    private static void solve(HashMap<Integer, Integer> cur, int sum, int dept, int index) {
        if (dept == k) {
            int tsum = sum + (int) Math.pow(cur.size(), 2);
            max = Math.max(tsum, max);
            return;
        }
        for (int i = index; i < n; i++) {
            cur.put(items[i][1], cur.getOrDefault(items[i][1], 0) + 1);
            solve(cur, sum + items[i][0], dept + 1, i + 1);
            Integer count = cur.get(items[i][1]);
            if (count == 1) {
                cur.remove(items[i][1]);
            } else {
                cur.put(items[i][1], count - 1);
            }
        }

    }
}